/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.IdGen;
import com.kaaction.modules.mer.entity.OperationContent;
import com.kaaction.modules.mer.entity.OperationContentTag;
import com.kaaction.modules.sys.entity.User;
import com.kaaction.modules.sys.utils.UserUtils;
import com.kaaction.modules.mer.dao.MerTagDao;
import com.kaaction.modules.mer.dao.OperationContentDao;
import com.kaaction.modules.mer.dao.OperationContentTagDao;

/**
 * 运营内容Service
 * @author huanghaiming
 * @version 2017-07-03
 */
@Service
@Transactional(readOnly = true)
public class OperationContentService extends CrudService<OperationContentDao, OperationContent> {
	
	@Autowired
	MerTagDao  merTagDao;
	
	@Autowired
	SpuService spuService;
	
	@Autowired
	OperationContentTagDao operationContentTagDao;

	public OperationContent get(String id) {
		return super.get(id);
	}
	
	public List<OperationContent> findList(OperationContent operationContent) {
		return super.findList(operationContent);
	}
	
	public Page<OperationContent> findPage(Page<OperationContent> page, OperationContent operationContent) {
		return super.findPage(page, operationContent);
	}
	
	@Transactional(readOnly = false)
	public void save(OperationContent operationContent) {
		
		String[] tag = operationContent.getTag();
		if(tag!=null&&tag.length>0){
			
			List<Map<String,Object>> list = merTagDao.selectTag(Arrays.asList(tag));
			String tagName = "";
			if(list.size()>0){
				
				for(Map<String,Object>  map:list){
					tagName+=map.get("name")+",";
				}
				operationContent.setTagName(tagName.substring(0, tagName.length()-1));
			}
		}else{
			operationContent.setTagName("");
		}
		
		String id = operationContent.getId();
		
		if (StringUtils.isBlank(id)){
			String uuid = IdGen.uuid();
			operationContent.setId(uuid);
			User user = UserUtils.getUser();
			if (StringUtils.isNotBlank(user.getId())){
				operationContent.setUpdateBy(user);
				operationContent.setCreateBy(user);
			}
			operationContent.setUpdateDate(new Date());
			operationContent.setCreateDate(new Date());
			dao.insert(operationContent);
			insertTag(uuid,tag);
		}else{
			operationContentTagDao.deleteTag(id);
			operationContent.preUpdate();
			dao.update(operationContent);
			insertTag(id,tag);
		}
		
	}
	
	private void insertTag(String opercontentId, String[] tag){
		
		if(tag!=null&&tag.length>0){
			OperationContentTag operationContentTag =  new OperationContentTag();
			for(String t:tag){
				operationContentTag.preInsert();
				operationContentTag.setTagId(t);
				operationContentTag.setOpercontentId(opercontentId);
				operationContentTagDao.insert(operationContentTag);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(OperationContent operationContent) {
		super.delete(operationContent);
	}

	@Transactional(readOnly = false)
	public void updateIsShow(OperationContent operationContent) {
		super.dao.updateIsShow(operationContent);
	}

	public List<Map<String,Object>> selBanner() {
		return super.dao.selBanner();
	}


	public Map<String,Object> selSelectedContentDtl(String id) {
		 Map<String,Object> map = super.dao.selSelectedContentDtl(id);
		 if(map!=null&&map.size()>0){
			 if(StringUtils.isNotBlank(id)){
				 map.put("listSpu", spuService.selSup(id));
			 }
			 map.put("listOtherStories", super.dao.selOtherStories(id));
		 }
		
		return map;
	}

	public List<Map<String,Object>> selSelectedContent(Map<String, Object> map) {
		
		List<Map<String,Object>> listMap = Lists.newArrayList();
		
		List<Map<String,Object>> listOperPro= super.dao.selOperContent(map);
		
		if(listOperPro.size()>0){
			
			for(Map<String,Object> mapOperPro:listOperPro){
				
				String id = (String) mapOperPro.get("id");
				if(StringUtils.isNotBlank(id)){
					mapOperPro.put("listSpu",spuService.selSup(id));
					listMap.add(mapOperPro);
				}
			}
		}
		
		return  listMap;
	}

	public List<Map<String, Object>> selTagContent(Map<String, Object> map) {
		
		String tagId = (String) map.get("tagId");
		List<Map<String, Object>> listMap = Lists.newArrayList();
		
		if(StringUtils.isNotBlank(tagId)){
			
			String[] tagIds = tagId.split(",");
			
			map.put("list", Arrays.asList(tagIds));
			List<Map<String, Object>>  ListTag = super.dao.selTagContent(map);
			
			for(Map<String, Object> mapTag :ListTag){
				String id = (String) mapTag.get("id");
				if(StringUtils.isNotBlank(id)){
					mapTag.put("listSpu",spuService.selSup(id));
				}
				listMap.add(mapTag);
			}
			return listMap;
			
		}else{
			
			List<Map<String, Object>>  ListOprCont = super.dao.selOprCont(map);
			
			for(Map<String, Object> mapOprCont :ListOprCont){
				String id = (String) mapOprCont.get("id");
				if(StringUtils.isNotBlank(id)){
					mapOprCont.put("listSpu",spuService.selSup(id));
				}
				listMap.add(mapOprCont);
			}
			return listMap;
		}
	}
}